package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.czl.module_base.bean.ListBean;
import com.czl.module_base.fragment.BaseRecyclerViewFragment;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.inventory.add.InventoryAddActivity;
import com.czl.module_storehouse.activity.inventory.details.InventoryDetailsActivity;
import com.czl.module_storehouse.activity.inventory.select.InventorySelectActivity;
import com.czl.module_storehouse.adapter.InventoryListAdapter;
import com.czl.module_storehouse.bean.InventoryBean;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.databinding.FragmentReceiveBinding;
import com.czl.module_storehouse.event.InventoryAddEvent;
import com.czl.module_storehouse.event.InventoryEvent;
import com.czl.module_storehouse.event.InventorySuccessEvent;
import com.czl.module_storehouse.event.InventoryUpdateEvent;
import com.czl.module_storehouse.mvp.presenter.InventoryPresenter;
import com.czl.module_storehouse.mvp.view.InventoryView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author by xubin,
 * @date on 2022/2/10.
 * PS: Not easy to write code, please indicate.
 */
public class InventoryFragment extends BaseRecyclerViewFragment<FragmentReceiveBinding> implements InventoryView {
    private InventoryListAdapter mAdapter;
    private int mState;
    @InjectPresenter
    InventoryPresenter mInventoryPresenter;

    public static InventoryFragment newInstance(int state) {
        Bundle args = new Bundle();
        args.putInt("state", state);
        InventoryFragment fragment = new InventoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FragmentReceiveBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return null;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mState = arguments.getInt("state");
        }
        if (mState == 0) {
            CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), mRecyclerViewBinding.containerBottom, true);
            buttonBinding.button.setText("添加盘点单");
            buttonBinding.button.setOnClickListener(v -> startActivity(new Intent(requireContext(), InventoryAddActivity.class)));
        }
        mAdapter = new InventoryListAdapter(R.layout.item_inventory_list, new ArrayList<>());
        mAdapter.setOnItemClickListener((adapter, view, position) -> goInventoryDetailsActivity(mAdapter.getItem(position)));
        mRecyclerViewBinding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);

        LiveEventBus.get(InventorySuccessEvent.class).observe(this, inventorySuccessEvent -> reload());
    }

    @Override
    protected void reload() {
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean refresh) {
        if (mState == 0) {
            if (mInventoryPresenter != null) {
                mInventoryPresenter.getNeedDealtInvenList(refresh);
            }
        } else {
            if (mInventoryPresenter != null) {
                mInventoryPresenter.getDealtInvenList(refresh);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(InventoryAddEvent event) {
        //添加盘点单
        if (event != null) {
            reload();
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(InventoryUpdateEvent event) {
        //更新盘点单
        if (event != null && !event.isCompleted()) {
            reload();
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mInventoryPresenter.getPageNo(), (ListBean<InventoryBean>) t);
    }

    private void goInventoryDetailsActivity(InventoryBean bean) {
        EventBus.getDefault().postSticky(new InventoryEvent(bean));
        if (mState == 0) {
            if (bean.getNowDateLong() < bean.getBeginDateLong()) {
                showToast("盘点时间未开始");
            } else {
                startActivity(new Intent(getContext(), InventorySelectActivity.class));
            }
        } else {
            EventBus.getDefault().postSticky(new InventoryEvent(bean));
            startActivity(new Intent(getContext(), InventoryDetailsActivity.class));
        }
    }

}
